/**
* @authors <darl2ng@gmail.com, ngocanh.ngo@gmail.com>
* @version 0.9
* Copyright 2010
* 
* The MIT License
* Copyright (c) 2010 <darl2ng@gmail.com, ngocanh.ngo@gmail.com>
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package  {

	import com.ng.ResHelp.c_ResLoader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.system.Security;
	
	public class c_PreLoader extends EventDispatcher {
		
		public static var COMPLETED:String = "c_Preloader::COMPLETED";
		private var __Preloader:MovieClip;		
		private var __Animation:c_LoaderAnimation;		
		private var __ResLoader:c_ResLoader;
		private var __BytesTotal:Number = 0;
		private var __SavedBytesLoaded:Number = 0;
		
		public function c_PreLoader(mc:MovieClip) {
			
			__Preloader = mc;
			__Animation= new c_LoaderAnimation(50,50);
			__Preloader.addChild(__Animation);			
			trace('Loader size: ' + __Preloader.loaderInfo.bytesTotal);
			trace('loaded at the moment of external loading start: '+__Preloader.loaderInfo.bytesLoaded);			
			initResLoader();		
		}

		public function get ResLoader():c_ResLoader {
			return __ResLoader;
		}		
		public function get animation():c_LoaderAnimation {
			return __Animation;
		}
		
		private function get isLoaded():Boolean {
			return (__Preloader.framesLoaded == __Preloader.totalFrames && __ResLoader.isLoaded);						
		}
		
		private function OnEnterFrame(event:Event):void	{
			if (isLoaded) {
				cleanUp();
				dispatchEvent(new Event(COMPLETED));
			} else {
				computeProgress();				
			}
		}		
		private function OnResEditorLoaded(event:Event) {
			__Preloader.addEventListener(Event.ENTER_FRAME, OnEnterFrame);
		}		
		private function cleanUp():void {
			__Preloader.removeEventListener(Event.ENTER_FRAME, OnEnterFrame);
			__Animation.clear();
			__Preloader.removeChild(__Animation);			
		}
		/**
		 * We consider here that:
		 * - already loaded before this moment is not displayed
		 * - xml loading is quick enough
		 * - percent, starting from 0 to 100 indicates the actual resting download process
		 * 
		 * We can change this behavior later
		 */
		private function computeProgress():void {			
			if (__BytesTotal == 0) {
				__SavedBytesLoaded = __Preloader.loaderInfo.bytesLoaded;
				__BytesTotal = __ResLoader.externalBytesTotal + __Preloader.loaderInfo.bytesTotal - __SavedBytesLoaded;
				trace('already loaded: ' + __SavedBytesLoaded);
				trace('external+application resting : '+__BytesTotal);
			}
			var l_loaded:Number=0;
			l_loaded = __ResLoader.externalBytesLoaded + __Preloader.loaderInfo.bytesLoaded - __SavedBytesLoaded;
			trace('loaded: ' + l_loaded);
			trace('on total : ' + (__ResLoader.externalBytesTotal +__ResLoader.PreparationBytesTotal + __Preloader.loaderInfo.bytesTotal));
			__Animation.update(l_loaded/__BytesTotal);
		}
		private function initResLoader():void {
			__ResLoader = c_ResLoader.GetInstance();
			
			switch (Security.sandboxType) {
				case Security.LOCAL_TRUSTED:
				case Security.LOCAL_WITH_FILE:
				case Security.LOCAL_WITH_NETWORK:
				__ResLoader.init("data/settings.xml");				
				break;
				case Security.REMOTE:
				__ResLoader.init("data/site/settings.xml");
				break;
			}			
			//__ResLoader.addEventListener(c_ResLoader.EXTERNAL_LIBRARIES_LOADED, OnResEditorLoaded);			
			__ResLoader.addEventListener(c_ResLoader.EXTERNAL_LIBRARIES_LOADING_STARTED, OnResEditorLoaded);			
		}
	}
}